﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using GauSmart.core;
using SharpMap.Data;
using SharpMap.Data.Providers;

namespace GauSmart
{
    public static class Helpers
    {
        private const string NDVI = "ndvi";
        private const string SLOPE = "slope_perc";

        public static List<Criteria> GetCriteriaList(string xmlFile)
        {
            XDocument root = XDocument.Load(xmlFile);
            List<Criteria> criterias;

            var xElement = root.Element("criterias");
            if (xElement != null)
            {
                criterias = 
                    (from cs in xElement.Elements("criteria")
                        select new Criteria
                        (
                            (CriteriaType)Enum.Parse(typeof(CriteriaType),(string)cs.Attribute("type")),
                            (string)cs.Attribute("name"),
                            (from ss in cs.Elements("suitability")
                                select new Suitability(
                                    (string)ss.Attribute("of"),
                                    (bool)ss.Element("asc"),
                                    (double)ss.Element("least"),
                                    (double)ss.Element("less"),
                                    (double)ss.Element("suitable"),
                                    (double)ss.Element("most"),
                                    (double)ss.Element("more")
                            )).Cast<ISuitability>().ToList()
                        )
                    ).ToList();
            }
            else
            {
                throw new Exception("Invalid file type.");
            }

            return criterias;
        }

        public static bool IsPotentialShape(ShapeFile potentialShape)
        {
            bool isPotentialShape;

            potentialShape.Open();

            FeatureDataRow potDataRow = 
                potentialShape.GetFeature(
                    potentialShape.GetObjectIDsInView(potentialShape.GetExtents())[0]
                );

            if (potDataRow.Table.Columns.Count < 4)
            {
                isPotentialShape = false;
            }
            else
            {
                if(potDataRow.Table.Columns[1].ColumnName.Equals(NDVI)
                    && potDataRow.Table.Columns[3].ColumnName.Equals(SLOPE))
                {
                    isPotentialShape = true;
                }
                else
                {
                    isPotentialShape = false;
                }
            }

            return isPotentialShape;
        }
    }
}